package com.spzx.payment.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.response.AlipayTradeWapPayResponse;
import com.spzx.common.rabbit.constant.MqConst;
import com.spzx.common.rabbit.service.RabbitService;
import com.spzx.order.api.RemoteOrderService;
import com.spzx.order.domain.OrderInfo;
import com.spzx.order.domain.OrderItem;
import com.spzx.payment.service.IAlipayService;
import com.spzx.payment.service.IPaymentInfoService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * BelongProject: spzx-parent
 * BelongPackage: com.spzx.payment.service.impl
 * Description:
 *
 * @Author 段鑫涛
 * @Create 2025/6/8 20:44
 * @Version 1.0
 */
@Service
public class AlipayServiceImpl implements IAlipayService {


    @Autowired
    private RemoteOrderService remoteOrderService;

    @Autowired
    private RabbitService rabbitService;

    @SneakyThrows
    @Override
    public String submitAlipay(String orderNo) {
        // 阿里网关
        String serverUrl = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";
        // 请求格式
        String format = "json";
        // 请求编码
        String charset = "utf-8";
        // 我们的私钥
        String privteKey = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCEaUd6n1qB0VN9oIhIzy9H5CuGhk49rLGRN3S9eWcKepko1hc4fzAka3Hd6KvLwKgOwFWd57LJgWLDjCFpJr14o6pBZYpxzFEPwW5JjLFxTa+vf34BsuuyU5X2sqmQuBAcW4ieJO+2T0198jumKyZq4sS0s3yrxjxoKyjRg/vLqfpQx/x3EZZAZ5bCiVsdDHa9A9oaCG6o+G3u1xzJybD8L/0EzYJXXnDOOEl0fzGOQhw09cM9A/QhQFWAc9dPo5FYMq0Iuh53o18dwNZzZC0Y1og8L305QxzBKceF7U4IttZAL3hHA3s7WxPTLdUsqJrdh9OZwYumqdRH0o2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x9JFW6qOKZ4uWN1GdWGrxiOtDJT4Cu3bBDflnPjTsRcHpW2Qg+EivgqtBw6DEp7iD0I0v38cpuQlTUNoKK+aP02FvTWfm8t7YhuYFiPJGUZ+AollA333l1jYz2pgwGAOBy3jgJwCU00n/uyeKtuCwBbQ==";
        // 阿里的公钥
        String aliPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAgb1nvRzDUN8aRwBnprs9NCLoSyD/fj1yjyj+3xWcbSu87NXtbe4EyfvspFzlIHbcEQ5KwzoJrgGLh6pdRdmjhrns6hpeG/GWuBURQa1jG6dwtqtGZjsExyn/S+m6IHI2hpq8fMLAxCdpF4Z3aGO3mPNOHOx++4HFQUxb2xZGEOv4SaslwkyEeeaAE0EKopegQhANwUEU8Y9aiAKVty7gYBvQgyuakMwtIz5KlotU0H8zbUuPcH/eJkN12zUmf2h+5gUOUTDh6XQkfdizX082IbBvxS2Fb6m/20CQob22zVtZgiLBB394MvLQvq3pHurSTH3aq7JoLKpPkyMkcbGwewIDAQAB";
        // app_id
        String appId = "9021000149651864";
        // sign_type
        String signType = "RSA2";

        AlipayClient alipayClient = new DefaultAlipayClient(serverUrl,appId,privteKey,format,charset,aliPublicKey,signType);

        // 请求参数
        AlipayTradeWapPayRequest alipayTradeWapPayRequest = new AlipayTradeWapPayRequest();
        // 异步回调接口
        alipayTradeWapPayRequest.setNotifyUrl("http://u44a3f52.natappfree.cc/payment/alipay/notifyPay");
        // 同步回跳地址
        alipayTradeWapPayRequest.setReturnUrl("http://192.168.137.66/#/pages/money/paySuccess");

        // 根据orderNo查询订单
        OrderInfo orderInfo = remoteOrderService.getOrderByOrderNo(orderNo,"inner").getData();
        Map<String,Object> map = new HashMap<>();
        map.put("out_trade_no",orderNo);
        map.put("total_amount",orderInfo.getTotalAmount());
        List<OrderItem> orderItemList = orderInfo.getOrderItemList();
        String productName = "";
        for (OrderItem orderItem : orderItemList) {
            productName = productName + "," + orderItem.getSkuName();

        }
        map.put("subject",productName);
        map.put("product_code","QUICK_WAP_WAY");
        alipayTradeWapPayRequest.setBizContent(JSON.toJSONString(map));

        // 发送请求
        AlipayTradeWapPayResponse alipayTradeWapPayResponse = alipayClient.pageExecute(alipayTradeWapPayRequest);
        String form = alipayTradeWapPayResponse.getBody();

        // 保存支付信息
        System.out.println("保存支付信息");// TODO

        return form;
    }

    @Override
    public void notifyPay(Map<String, String> paramMap) {
        String orderNo = paramMap.get("out_trade_no");

        System.out.println("更新支付信息");// TODO

        System.out.println("通知MQ更新订单信息:"+orderNo);
        rabbitService.sendMessage(MqConst.EXCHANGE_PAYMENT_PAY,MqConst.ROUTING_PAYMENT_PAY,orderNo);
        System.out.println("通知MQ更新库存信息:"+orderNo);
        rabbitService.sendMessage(MqConst.EXCHANGE_PRODUCT,MqConst.ROUTING_MINUS,orderNo);
    }
}
